package com.ciwong.epaper.modules.msg.adapter;


import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.msg.bean.MessageInfo;
import com.ciwong.epaper.modules.msg.bean.MsgTypeConfig;
import com.ciwong.epaper.modules.msg.util.MsgDateFormat;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;


/**
 * Created by res-yinxue on 2016/6/16.
 */
public class MyMessageAdapter extends BaseAdapter {
    /**
     * 上下文对象
     */
    private Context mContext;
    /**
     * 消息列表
     */
    private List<MessageInfo> infos;

    /**
     * 构造方法
     *
     * @param context 上下文对象
     * @param infos   消息列表
     */
    public MyMessageAdapter(Context context, List<MessageInfo> infos) {
        this.mContext = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos == null ? 0 : infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos == null || infos.isEmpty() ? null : infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_msg, null);

            holder.iv_msg_icon = (SimpleDraweeView) convertView.findViewById(R.id.iv_msg_icon);

            holder.titleTv = (TextView) convertView.findViewById(R.id.msg_title_tv);
            holder.contentTv = (TextView) convertView.findViewById(R.id.msg_content_tv);
            holder.timeTv = (TextView) convertView.findViewById(R.id.msg_time_tv);
            holder.numTv = (TextView) convertView.findViewById(R.id.msg_num_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (infos.get(position).getMsgGroup() == MsgTypeConfig.MSG_SERVICE) {
            holder.titleTv.setText("站内信");
        } else {
            holder.titleTv.setText("作业消息");
        }

        holder.contentTv.setText(infos.get(position).getMsgIntr());
        holder.timeTv.setText(MsgDateFormat.formatDate(infos.get(position).getPublishDate() * 1000));


        //暂时设置为默认班级头像
        //String logoUrl = infos.getLogoUrl();
//        ImageLoader.getInstance().displayImage(
//                "drawable://" + R.drawable.about_icon,
//                holder.iv_msg_icon
//        );

        setLocalDefaultImg( holder.iv_msg_icon,infos.get(position).getMsgGroup());

        //消息次数，当大于1次的时候才显示
        int num = infos.get(position).getMsgCount();
        if (num > 0 && num < 100) {
            holder.numTv.setText(num + "".trim());
            holder.numTv.setVisibility(View.VISIBLE);
        } else if (num >= 100) {
            holder.numTv.setText("99");
            holder.numTv.setVisibility(View.VISIBLE);
        } else {
            holder.numTv.setVisibility(View.GONE);
        }


        return convertView;
    }

    private static class ViewHolder {
        SimpleDraweeView iv_msg_icon;
        TextView titleTv, contentTv, timeTv, numTv;
    }


    private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView,int msgGroupType) {
        Uri uri;
        if(msgGroupType==MsgTypeConfig.MSG_SERVICE){
            uri = Uri.parse("res:///" + R.mipmap.icon_msg_service);
        }else{
            uri= Uri.parse("res:///" + R.mipmap.icon_msg_homework);
        }

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setTapToRetryEnabled(true)
                .build();
        simpleDraweeView.setController(controller);
    }
}
